package homework12月9号补交.homework2.d11;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

public class test {
    public static void main(String[] args) throws Exception {
        List<Animal> run = run();
        System.out.println(run.toString());
    }
    public static List<Animal> run() throws Exception {
        ArrayList<Animal> A = new ArrayList<>();
        List<String> list = FileUtils.readLines(new File("D:\\ldea dashuju\\day.12.6\\src\\czxy\\homework2\\d11\\dataAnimal.txt"));
        for (String s : list) {
            System.out.println(s);
            String[] arr = s.split(" ");
            System.out.println(arr[0]);
            Class<?> aClass = Class.forName(arr[0]);
            Constructor<?> constructor = aClass.getConstructor();
            Object o = constructor.newInstance();
                System.out.println(arr[1]);
                if (o instanceof Cat){
                    Cat o1 = (Cat) o;
                    o1.setId(arr[1].split("=")[1]);
                    o1.setName(arr[2].split("=")[1]);
                    o1.setColor(arr[3].split("=")[1]);
                    A.add(o1);
                }else {
                    Dog o1 = (Dog) o;
                    o1.setId(arr[1].split("=")[1]);
                    o1.setName(arr[2].split("=")[1]);
                    o1.setAge(Integer.parseInt(arr[3].split("=")[1]));
                    A.add(o1);
            }
        }
        return A;
    }
}
